define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cms/news/index' + location.search,
                    add_url: 'cms/news/add',
                    edit_url: 'cms/news/edit',
                    del_url: 'cms/news/del',
                    multi_url: 'cms/news/multi',
                    import_url: 'cms/news/import',
                    table: 'news',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
//                        {field: 'parent_id', title: __('Channels_id')},channel_text
                        {field: 'channel_text', title: __('Channels_id')},
                        {field: 'post_title', title: __('Post_title'), operate: 'LIKE'},
                        {field: 'thumbnail', title: __('Thumbnail'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},

//                        {field: 'post_type', title: __('Post_type')},
//                        {field: 'post_format', title: __('Post_format')},
                        {field: 'user_text', title: __('User_id')},
//                        {field: 'post_status', title: __('Post_status')},

                        {
                            field: 'iscomment',
                            title: __('Comment_status'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'is_top',
                            title: __('Is_top'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
                        {
                            field: 'status',
                            title: __('审核'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle
                         },
//                        {field: 'is_top', title: __('Is_top')},
//                        {field: 'recommended', title: __('Recommended')},
                        {
                            field: 'recommended',
                            title: __('Recommended'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle
                        },
//                        {field: 'post_hits', title: __('Post_hits')},
                        {field: 'post_favorites', title: __('Post_favorites')},
                        {field: 'post_forward', title: __('Post_forward')},
//                        {field: 'post_like', title: __('Post_like')},
                        {field: 'comment_count', title: __('Comment_count')},
                        {field: 'status_text', title:__('Status'), formatter:function (value, row, index) {
                                console.log('==',value)
                          if(value =='隐藏'){
                              return  '<span class="text-gray"><i class="fa fa-circle"></i> '+value+'</span>';
                          }else{
                                return  '<span class="text-success"><i class="fa fa-circle"></i> '+value+'</span>';
                          }
//                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                          }
                        },
                        {field: 'membership_price', title: __('Membership_price')},
                        {field: 'membership_duration', title: __('Membership_duration')},

                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'published_time', title: __('Published_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
//                        {field: 'post_keywords', title: __('Post_keywords'), operate: 'LIKE'},
//                        {field: 'post_excerpt', title: __('Post_excerpt'), operate: 'LIKE'},
//                        {field: 'post_source', title: __('Post_source'), operate: 'LIKE'},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on('click', '.btn-move', function () {
                var ids = Table.api.selectedids(table);
                Layer.open({
                    title: __('Move'),
                    content: Template("channeltpl", {}),
                    btn: [__('Move')],
                    yes: function (index, layero) {
                        var channel_id = $("select[name='channel']", layero).val();
                        if (channel_id == 0) {
                            Toastr.error(__('Please select channel'));
                            return;
                        }
                        Fast.api.ajax({
                            url: "cms/news/move/ids/" + ids.join(","),
                            type: "post",
                            data: {channel_id: channel_id},
                        }, function () {
                            table.bootstrapTable('refresh', {});
                            Layer.close(index);
                        });
                    },
                    success: function (layero, index) {
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
